/**
 * Created by radoslaw.panczyk on 2014-12-11.
 */
var express = require('express');
var mongoose = require('mongoose');

var router = express.Router();

var Question = mongoose.model('Question');
var Answer = mongoose.model('Answer');
var Quiz = mongoose.model('Quiz');

//wyszukiwanie pytania o zadanym w parametrze id
router.param('question', function(req, res, next, id) {
   var query = Question.findById(id);

    query.exec(function(err, question) {
       if(err) { return next(err); }
       if(!question) { return next(new Error('Nie znaleziono pytania!')); }

       req.question = question;
       return next();
    });
});

//wyszukiawnie możliwej odpowiedzi o zadanym parametrze id
router.param('answer', function(req, res, next, id) {
   var query = Answer.findById(id);

    query.exec(function(err, answer) {
        if(err) { return next(err); }
        if(!answer) { return next(new Error('Nie znaleziono odpowiedzi!')); }

        req.answer = answer;
        return next();
    })
});

//wyszukiwanie quizu o zadanym w parametrze id
router.param('quiz', function(req, res, next, id) {
    var query = Quiz.findById(id);

    query.exec(function(err, quiz) {
        if(err) { return next(err); }
        if(!quiz) { return next(new Error('Nie znaleziono quizu!')); }

        req.quiz = quiz;
        return next();
    });
});

//to jest potrzebne inaczej nie można wywoływać usług z tego routingu
router.get('/', function(req, res) {
    //do nothing
});

//pobranie szczegółów o pytaniu
router.get('/:question', function(req, res) {
    req.question.populate('answers', function(err, question) {
        res.json(question);
    });
});

//dodawanie nowego pytania do quizu
router.post('/:quiz/new', function(req, res) {
    var question = new Question(req.body);

    question.quiz = req.quiz;

    question.save(function(err, question) {
        if(err) { return next(err); }

        req.quiz.questions.push(question);
        req.quiz.save(function(err, quiz) {
            if(err) { return next(err); }
            res.json(question);
        });
    })
});

//dodawnie (możliwości) odpowiedzi do pytania
router.post('/:question/add', function(req, res) {
    var answer = new Answer(req.body);

    answer.question = req.question;

    answer.save(function(err, answer) {
       if(err) { return next(err); }

        req.question.answers.push(answer);
        req.question.save(function(err, quiz) {
           if(err) { return next(err); }
            res.json(answer);
        });
    });
});

//usuwanie pytan
router.delete('/:question', function(req, res) {
    Question.remove({
        _id: req.params.question
    }, function(err, question) {
        if(err) { return res.send(err); }
        res.json({message: 'Sukces: usuniecie pytania'});
    });
})

//usuwanie odpowiedzi
router.delete('/del/:answer', function(req, res) {
    Answer.remove({
        _id: req.params.answer
    }, function(err, answer) {
        if (err) { return res.send(err); }

        res.json({ message: 'Sukces: usuniecie odpowiedzi'});
    });
});

module.exports = router;